﻿using System.Data;
using System;
using System.IO;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;


namespace PMS.Utilities
{
    public class Utility
    {
        public static string ConnectionString = "";
        public static string ConnectioNStringTemplate = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";
        public static string LogPath = "~/Error/Log.txt";


        private static int _semesterID = 0;
        private static int _UserID = 0;
        private static string _UserName = "Guest";
        private static string _Role = "Guest";
        private static int _RoleID;

        public static int UserID
        {
            get
            {
                _UserID = 0;
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    string cookieName = FormsAuthentication.FormsCookieName;
                    HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (authCookie != null)
                    {
                        System.Web.Security.FormsAuthenticationTicket authTicket = null;
                        authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
                        _UserID = Convert.ToInt32(authTicket.Name.Split(';')[0]);
                    }
                }
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }
        public static string UserName
        {
            get
            {
                _UserName = "Guest";
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    string cookieName = FormsAuthentication.FormsCookieName;
                    HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (authCookie != null)
                    {
                        System.Web.Security.FormsAuthenticationTicket authTicket = null;
                        authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
                        _UserName = authTicket.Name.Split(';')[1];
                    }
                }
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        public static string Role
        {
            get
            {
                _Role = "Guest";
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    string cookieName = FormsAuthentication.FormsCookieName;
                    HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (authCookie != null)
                    {
                        System.Web.Security.FormsAuthenticationTicket authTicket = null;
                        authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
                        _Role = authTicket.Name.Split(';')[2];
                    }
                }
                return _Role;
            }
            set
            {
                _Role = value;
            }
        }
        public static int RoleID
        {
            get
            {
                _RoleID = 0;
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    string cookieName = FormsAuthentication.FormsCookieName;
                    HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (authCookie != null)
                    {
                        System.Web.Security.FormsAuthenticationTicket authTicket = null;
                        authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
                        _RoleID = Convert.ToInt32(authTicket.Name.Split(';')[3]);
                    }
                }
                return _RoleID;
            }
            set
            {
                _RoleID = value;
            }
        }
        public static int semesterID
        {
            get
            {
                _semesterID = 0;
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    string cookieName = FormsAuthentication.FormsCookieName;
                    HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                    if (authCookie != null)
                    {
                        System.Web.Security.FormsAuthenticationTicket authTicket = null;
                        authTicket = System.Web.Security.FormsAuthentication.Decrypt(authCookie.Value);
                        _semesterID = Convert.ToInt32(authTicket.Name.Split(';')[4]);
                    }
                }
                return _semesterID;
            }
            set
            {
                _semesterID = value;
            }
        }

        public static CultureInfo cultureInfo;
        public static string DateTimeFormat = "dd/MM/yyyy";
        public static string TimeFormat = "hh:mm tt";

        public static bool FieldExist(DataTable dt, string fieldName)
        {
            dt.DefaultView.RowFilter = string.Format("ColumnName= '{0}'", fieldName);
            return (dt.DefaultView.Count > 0);
        }

        #region Convert
        public static long LongParse(object obj)
        {
            if (obj == null) { return 0; }

            long result;
            if (long.TryParse(obj.ToString(), out result)) { }

            return result;
        }

        public static int IntParse(object obj)
        {
            if (obj == null) { return 0; }

            int result;
            if (int.TryParse(obj.ToString(), out result)) { }

            return result;
        }

        public static int IntParse(bool obj)
        {
            return !obj ? 0 : 1;
        }

        public static decimal DecimalParse(object obj)
        {
            if (obj == null) { return 0; }

            decimal result;
            if (decimal.TryParse(obj.ToString(), NumberStyles.Any, cultureInfo, out result)) { }

            return result;
        }

        public static bool BoolParse(object obj)
        {
            if (obj == null) { return false; }

            bool result;
            if (bool.TryParse(obj.ToString(), out result)) { }

            return result;
        }

        public static DateTime DateTimeParse(object obj)
        {
            if (obj == null) { return DateTime.Now; }

            DateTime result;
            if (DateTime.TryParse(obj.ToString(), out result))
            {
                return result;
            }

            return DateTime.Now;
        }

        public static string StringParse(object obj)
        {
            return obj == null ? String.Empty : obj.ToString();
        }

        public static string StringParse(int[] obj)
        {
            string result = string.Empty;

            for (int i = 0; i < obj.Length; i++)
            {
                result += obj[i].ToString() + ",";
            }

            if (result.EndsWith(","))
            {
                result = result.Remove(result.Length - 1);
            }

            return result;
        }

        #endregion

        #region Write Log

        public static void LogEx(string exCode, Exception ex)
        {
            StreamWriter sw = new StreamWriter(LogPath, true);
            sw.Close();
            StreamReader sr = new StreamReader(LogPath);
            string txt = sr.ReadToEnd();
            sr.Close();
            sw = new StreamWriter(LogPath);
            sw.WriteLine(txt);
            sw.WriteLine("Error Code: " + exCode);
            sw.WriteLine("Message: " + ex.Message);
            sw.WriteLine(ex.StackTrace);
            sw.WriteLine("-------------------------------------------------------------------------------");

            sw.Close();
        }

        #endregion

        #region Escape Special char

        public static string EscapeSQLChar(string str)
        {
            str = str.Replace("'", "''");
            return str;
        }

        #endregion

        #region Encrypt

        public static string GetMD5Hash(string input)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2").ToLower());
            }
            string password = sb.ToString();
            return password;
        }

        #endregion


        public static string GetCurrentHost()
        {
            return HttpContext.Current.Request.Url.Scheme + System.Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Host +
            (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port);
        }



    }
}
